﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using LayuiDemo.Models;


namespace LayuiDemo.tms.ViewModels.TruckVMs
{
    public partial class TruckListVM : BasePagedListVM<Truck_View, TruckSearcher>
    {
        protected override List<GridAction> InitGridAction()
        {
            return new List<GridAction>
            {
                this.MakeStandardAction("Truck", GridActionStandardTypesEnum.Create, Localizer["Sys.Create"],"tms", dialogWidth: 800),
                this.MakeStandardAction("Truck", GridActionStandardTypesEnum.Edit, Localizer["Sys.Edit"], "tms", dialogWidth: 800),
                this.MakeStandardAction("Truck", GridActionStandardTypesEnum.Delete, Localizer["Sys.Delete"], "tms", dialogWidth: 800),
                this.MakeStandardAction("Truck", GridActionStandardTypesEnum.Details, Localizer["Sys.Details"], "tms", dialogWidth: 800),
                this.MakeStandardAction("Truck", GridActionStandardTypesEnum.BatchEdit, Localizer["Sys.BatchEdit"], "tms", dialogWidth: 800),
                this.MakeStandardAction("Truck", GridActionStandardTypesEnum.BatchDelete, Localizer["Sys.BatchDelete"], "tms", dialogWidth: 800),
                this.MakeStandardAction("Truck", GridActionStandardTypesEnum.Import, Localizer["Sys.Import"], "tms", dialogWidth: 800),
                this.MakeStandardAction("Truck", GridActionStandardTypesEnum.ExportExcel, Localizer["Sys.Export"], "tms"),
            };
        }


        protected override IEnumerable<IGridColumn<Truck_View>> InitGridHeader()
        {
            return new List<GridColumn<Truck_View>>{
                this.MakeGridHeader(x => x.TruckName),
                this.MakeGridHeader(x => x.DriverName_view),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override IOrderedQueryable<Truck_View> GetSearchQuery()
        {
            var query = DC.Set<Truck>()
                .CheckContain(Searcher.TruckName, x=>x.TruckName)
               // .CheckWhere(Searcher.SelectedDriverTrucksIDs,x=>DC.Set<DriverTruck>().Where(y=>Searcher.SelectedDriverTrucksIDs.Contains(y.DriverId)).Select(z=>z.TruckId).Contains(x.ID))
                .Select(x => new Truck_View
                {
				    ID = x.ID,
                    TruckName = x.TruckName,
                    DriverName_view = x.DriverTrucks.Select(y=>y.Driver.DriverName).ToSepratedString(null,","), 
                })
                .OrderBy(x => x.ID);
            return query;
        }

    }

    public class Truck_View : Truck{
        public String DriverName_view { get; set; }

    }
}
